// ValidationAspects
// Written by Mike Saunders
// Released under Microsoft Public License (Ms-PL)
using System.Linq;
using ValidationAspects.Exceptions;
using ValidationAspects.Factories;
using ValidationAspects.Sdk;

namespace ValidationAspects
{
	public static class ObjectValidation
	{
		public static ValidationResult Validate(this object instance)
		{
			Enforce.IsNotNull(instance, "instance");
			try
			{
				new ValidateObject().Create(instance.GetType()).Invoke(instance, null);
			}
			catch (ValidationContextException e)
			{
				// strip the ValidateObjectException
				ValidationContextException contextException = new ValidationContextException(e.Exceptions.Skip(1));
				contextException.SetContext(e.Context);

				return new ValidationResult(contextException);
			}
			return new ValidationResult();
		}
	}
}